home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-06-23 | 6.8 KB | 263 lines |
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
-
- /**
- * Apple Worldwide Developer Technical Support
- *
- * Sample showing how to send and receive AppleEvents using JDirect 2.
- *
- * File: AEReceiveFrame.java
- *
- * This class contains this sample's user interface for receiving AppleEvents.
- *
- * @author Levi Brown
- * @author Apple Computer, Inc.
- *
- * Copyright ©1999 Apple Computer, Inc.
- * All rights reserved.
- *
- * @version 1.0
- * 4/15/1999 Shipped as 'AppleEvent Send and Receive' sample.
- *
- * You may incorporate this sample code into your applications without
- * restriction, though the sample code has been provided "AS IS" and the
- * responsibility for its operation is 100% yours. However, what you are
- * not permitted to do is to redistribute the source as "Apple Sample
- * Code" after having made changes. If you're going to re-distribute the
- * source, we require that you make it clear in the source that the code
- * was descended from Apple Sample Code, but that you've made changes.
- */
- public class AEReceiveFrame extends Frame
- {
- public AEReceiveFrame()
- {
- //{{INIT_CONTROLS
- GridBagLayout gridBagLayout;
- gridBagLayout = new GridBagLayout();
- setLayout(gridBagLayout);
- setVisible(false);
- setSize(466,90);
- setBackground(new Color(-1052689));
- label1 = new java.awt.Label("Message:");
- label1.setBounds(5,0,50,21);
- label1.setFont(new Font("SansSerif", Font.BOLD, 9));
- GridBagConstraints gbc;
- gbc = new GridBagConstraints();
- gbc.gridx = 1;
- gbc.gridy = 1;
- gbc.gridwidth = 2;
- gbc.anchor = GridBagConstraints.WEST;
- gbc.fill = GridBagConstraints.NONE;
- gbc.insets = new Insets(0,5,0,0);
- ((GridBagLayout)getLayout()).setConstraints(label1, gbc);
- add(label1);
- label2 = new java.awt.Label("Status:");
- label2.setBounds(5,65,40,21);
- label2.setFont(new Font("SansSerif", Font.BOLD, 9));
- gbc = new GridBagConstraints();
- gbc.gridx = 1;
- gbc.gridy = 5;
- gbc.anchor = GridBagConstraints.WEST;
- gbc.fill = GridBagConstraints.NONE;
- gbc.insets = new Insets(0,5,0,0);
- ((GridBagLayout)getLayout()).setConstraints(label2, gbc);
- add(label2);
- divider1 = new Divider();
- divider1.setBounds(5,64,434,1);
- gbc = new GridBagConstraints();
- gbc.gridx = 1;
- gbc.gridy = 4;
- gbc.gridwidth = 2;
- gbc.weightx = 0.5;
- gbc.anchor = GridBagConstraints.WEST;
- gbc.fill = GridBagConstraints.HORIZONTAL;
- gbc.insets = new Insets(0,5,0,5);
- ((GridBagLayout)getLayout()).setConstraints(divider1, gbc);
- add(divider1);
- statusLabel = new java.awt.Label("Initializiing...");
- statusLabel.setBounds(50,65,394,21);
- statusLabel.setFont(new Font("SansSerif", Font.PLAIN, 9));
- gbc = new GridBagConstraints();
- gbc.gridx = 2;
- gbc.gridy = 5;
- gbc.weightx = 0.5;
- gbc.anchor = GridBagConstraints.WEST;
- gbc.fill = GridBagConstraints.HORIZONTAL;
- gbc.insets = new Insets(0,5,0,0);
- ((GridBagLayout)getLayout()).setConstraints(statusLabel, gbc);
- add(statusLabel);
- messageLabel = new java.awt.Label("");
- messageLabel.setBounds(10,21,434,21);
- messageLabel.setFont(new Font("SansSerif", Font.PLAIN, 9));
- gbc = new GridBagConstraints();
- gbc.gridx = 1;
- gbc.gridy = 2;
- gbc.gridwidth = 2;
- gbc.weightx = 0.5;
- gbc.anchor = GridBagConstraints.WEST;
- gbc.fill = GridBagConstraints.HORIZONTAL;
- gbc.insets = new Insets(0,10,0,0);
- ((GridBagLayout)getLayout()).setConstraints(messageLabel, gbc);
- add(messageLabel);
- panel1 = new java.awt.Panel();
- panel1.setLayout(null);
- panel1.setBounds(0,42,444,22);
- gbc = new GridBagConstraints();
- gbc.gridx = 1;
- gbc.gridy = 3;
- gbc.gridwidth = 2;
- gbc.weightx = 0.5;
- gbc.weighty = 0.5;
- gbc.fill = GridBagConstraints.BOTH;
- gbc.insets = new Insets(0,0,0,0);
- ((GridBagLayout)getLayout()).setConstraints(panel1, gbc);
- add(panel1);
- setTitle("AEReceive");
- //}}
-
- //{{INIT_MENUS
- //}}
-
- //{{REGISTER_LISTENERS
- SymWindow aSymWindow = new SymWindow();
- this.addWindowListener(aSymWindow);
- //}}
-
- setLocation(50, 180);
-
- //Create a new timer to handle status messages.
- timer = new Timer(kEraseSleep, new TimerCallback()
- {
- public void timeIsUp()
- {
- isErase = false;
- try
- {
- setStatus("Idle");
- }
- finally
- {
- isErase = true;
- }
- }
- });
-
- isErase = true;
-
- //Create a new AEReceive object so we can register out AppleEvent handler
- aeReceive = new AEReceive();
- //Register a new instance of our ActionListener with the AEReceive object
- //so we get notified when an AppleEvent is received.
- aeReceive.addActionListener(new Action());
- }
-
- /**
- * The entry point to our application
- */
- public static void main(String[] args)
- {
- (new AEReceiveFrame()).setVisible(true);
- }
-
- /**
- * Gets called by the framework when this component is added to the component hierarchy.
- * Overriden here to install our AppleEvent handler.
- */
- public void addNotify()
- {
- super.addNotify();
-
- setStatus("Installing AppleEvent handler...");
- try
- {
- aeReceive.installAEHandler();
- }
- catch (NativeException exc)
- {
- setStatus("Failed to install AppleEvent handler. (#" + exc.getErrNum() + ")");
- return;
- }
-
- setStatus("AppleEvent handler successfully installed.");
- }
-
- /**
- * Set the text of the status label
- * @param the message to use.
- */
- protected void setStatus(String message)
- {
- statusLabel.setText(message);
- if (isErase)
- timer.start();
- }
-
- /**
- * Set the text of the message label
- * @param the message to use.
- */
- protected void setMessage(String message)
- {
- messageLabel.setText(message);
- }
-
- //{{DECLARE_CONTROLS
- java.awt.Label label1;
- java.awt.Label label2;
- Divider divider1;
- java.awt.Label statusLabel;
- java.awt.Label messageLabel;
- java.awt.Panel panel1;
- //}}
-
- //{{DECLARE_MENUS
- //}}
-
- class SymWindow extends java.awt.event.WindowAdapter
- {
- public void windowClosing(java.awt.event.WindowEvent event)
- {
- Object object = event.getSource();
- if (object == AEReceiveFrame.this)
- AEReceiveFrame_WindowClosing(event);
- }
- }
-
- void AEReceiveFrame_WindowClosing(java.awt.event.WindowEvent event)
- {
- setVisible(false); // hide the AEReceiveFrame
- dispose(); // free the system resources
- System.exit(0); // close the application
- }
-
- /**
- * An inner class designed to listen for ActionEvents
- * from the AEReceive class.
- */
- class Action implements ActionListener
- {
- public void actionPerformed(ActionEvent event)
- {
- setMessage(event.getActionCommand());
- setStatus("Received AppleEvent.");
- }
- }
-
- /**
- * The time in milliseconds to wait before removing the previous status message.
- */
- protected static final int kEraseSleep = 1250;
- /**
- * Our AEReceive object which allows us to register the AppleEvent Handler
- */
- protected AEReceive aeReceive;
- /**
- * An internal flag to prevent recursion in the status message handling code.
- */
- protected boolean isErase;
- /**
- * A timer to keep track of displaying the status message.
- */
- protected Timer timer;
- }